package cn.ymatou.paymentproxy.admin.api.testcase;

import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import cn.ymatou.paymentproxy.admin.api.parameter.PaymentQueryBean;
import cn.ymatou.paymentproxy.admin.api.service.PaymentQueryCall;

import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.sqlwapper.DefaultWapper;

/**
 * 支付相关查询接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_PaymentQuery {
	private static PaymentQueryBean paymentqueryBean;
	private static PaymentQueryCall paymentqueryCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("支付相关查询接口");
	}

	@Before
	public void caseUp() throws JSONException {
		paymentqueryBean = new PaymentQueryBean();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(paymentqueryCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_PaymentQuery_001() {
		Logger.start(true, "queryType=10:useOrderId-FromPaymentNotify");
		try {
			paymentqueryCall = new PaymentQueryCall("10");
//				paymentqueryBean.setQueryId(item.get("orderid").toString());
				paymentqueryCall.setData(paymentqueryBean);
				paymentqueryCall.callService();


		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_PaymentQuery_002() {
		Logger.start(true, "queryType=10:useOrderId-FromTradingItem");
		try {
			paymentqueryCall = new PaymentQueryCall("10");
			List<Map> list = new DefaultWapper().queryInstitutePaymentsByOrderFromYmtTradingItem();
			if (list.size() >= 1) {
				Map item = list.get(0);
				paymentqueryBean.setQueryId(item.get("orderid").toString());
				paymentqueryCall.setData(paymentqueryBean);
				paymentqueryCall.callService();

				Logger.comment("paymentqueryCall send:" + paymentqueryCall.getEntityString());
				Logger.comment("paymentqueryCall response:" + paymentqueryCall.getReturnData());

				StringBuffer sb = new StringBuffer();
				sb.append("{\"InstitutePaymentId\":\"");
				sb.append(item.get("InstituPaymentId").toString());
				sb.append("\",\"Amount\":");
				double amount = Double.parseDouble(item.get("Amount").toString());
				sb.append(String.format("%.2f", amount));
				sb.append(",\"TradingNo\":\"");
				sb.append(item.get("TradingNo").toString());
				Logger.comment(sb.toString());
				Logger.verifyEquals(true, paymentqueryCall.getReturnData().contains(sb.toString()), "验证返回");

			} else {
				Logger.comment("数据库未查到合适数据。");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_PaymentQuery_003() {
		Logger.start(true, "queryType=10:useOrderId-FromYmtPostPayTradingInfo");
		try {
			paymentqueryCall = new PaymentQueryCall("10");
			List<Map> list = new DefaultWapper().queryInstitutePaymentsByOrderFromYmtPostPayTradingInfo();
			if (list.size() >= 1) {
				Map item = list.get(0);
				paymentqueryBean.setQueryId(item.get("orderid").toString());
				paymentqueryCall.setData(paymentqueryBean);
				paymentqueryCall.callService();

				Logger.comment("paymentqueryCall send:" + paymentqueryCall.getEntityString());
				Logger.comment("paymentqueryCall response:" + paymentqueryCall.getReturnData());

				StringBuffer sb = new StringBuffer();
				sb.append("{\"InstitutePaymentId\":\"");
				sb.append(item.get("InstituPaymentId").toString());
				sb.append("\",\"Amount\":");
				double amount = Double.parseDouble(item.get("Amount").toString());
				sb.append(String.format("%.2f", amount));
				sb.append(",\"TradingNo\":\"");
				sb.append(item.get("TradingNo").toString());
				Logger.comment(sb.toString());
				Logger.verifyEquals(true, paymentqueryCall.getReturnData().contains(sb.toString()), "验证返回");

			} else {
				Logger.comment("数据库未查到合适数据。");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_PaymentQuery_004() {
		Logger.start(true, "queryType=30.UseInstitutionPaymentIdForOrderId-FromPaymentNotify");
		try {
			paymentqueryCall = new PaymentQueryCall("30");
			Map m = new DefaultWapper().queryIPayment30();
			String p30= (String) m.get("PaymentQuery30");
			paymentqueryBean.setQueryId(p30);
			paymentqueryCall.setData(paymentqueryBean);
			paymentqueryCall.callService();
			//通过InstitutionPaymentId获取订单号
			String orderid=paymentqueryCall.getReturnData();
			Logger.verifyNotNull(orderid, "返回结果非空");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_PaymentQuery_005() {
		Logger.start(true, "queryType=30.UseInstitutionPaymentIdForOrderId-FromYmtTradingItem");
		try {
			paymentqueryCall = new PaymentQueryCall("30");
			List<Map> list = new DefaultWapper().queryOrderByPaymentFromYmtTradingItem();
			if (list.size() >= 1) {
				Map item = list.get(0);
				paymentqueryBean.setQueryId(item.get("TradeNo").toString());
				paymentqueryCall.setData(paymentqueryBean);
				paymentqueryCall.callService();

				Logger.comment("paymentqueryCall send:" + paymentqueryCall.getEntityString());
				Logger.comment("paymentqueryCall response:" + paymentqueryCall.getReturnData());

				Logger.verifyEquals(item.get("orderid").toString(), paymentqueryCall.getReturnData(), "验证返回");

			} else {
				Logger.comment("数据库未查到合适数据。");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_PaymentQuery_006() {
		Logger.start(true, "queryType=30.UseInstitutionPaymentIdForOrderId-FromYmtPostPayTradingInfo");
		try {
			paymentqueryCall = new PaymentQueryCall("30");
			List<Map> list = new DefaultWapper().queryOrderByPaymentFromYmtPostPayTradingInfo();
			if (list.size() >= 1) {
				Map item = list.get(0);
				paymentqueryBean.setQueryId(item.get("TradeNo").toString());
				paymentqueryCall.setData(paymentqueryBean);
				paymentqueryCall.callService();

				Logger.comment("paymentqueryCall send:" + paymentqueryCall.getEntityString());
				Logger.comment("paymentqueryCall response:" + paymentqueryCall.getReturnData());

				Logger.verifyEquals(item.get("orderid").toString(), paymentqueryCall.getReturnData(), "验证返回");

			} else {
				Logger.comment("数据库未查到合适数据。");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}